<?php

include_once($mcrp . 'functions/users.php');
include_once($mcrp . 'functions/security.php');
include_once($mcrp . 'functions/sql.php');
include_once($mcrp . 'functions/log.php');
include_once($mcrp . 'functions/login.php');
include_once($mcrp . 'functions/language.php');
define('PUN_ROOT', $mcrp . 'forum/');
include_once($mcrp . 'forum/include/common.php');

define('MIN_VIEW_WAIT', 30);

Session_start();
Secure();

$defaultuser = new UserInfos();
global $mcsql;
$mcsql = new SQL;

global $lang;
$lang['lang'] = LoadLanguage();

global $mcuser;
$mcuser = $defaultuser;

if(isset($_POST['contestlogin']) && isset($_POST['username']) && isset($_POST['password']) && defined('MC_CONTEST'))
{
	if(LoginUserContest($_POST['username'], $_POST['password']) !== true)
	{
		echo 'Erreur Auth';
		exit;
	}
}
else
{
	if(defined('MC_CONTEST'))
	{
		if(AutoLoginUserContest() == false)
		{
			AutoLoginUser();
			if($mcuser->id == 0) exit;
			$str = basename($_SERVER['SCRIPT_NAME']);
			if($str != 'contest.php') exit;
			unset($_SESSION['var']);
			if(isset($_SESSION['onlyview_time']) && (time() - $_SESSION['onlyview_time'] < MIN_VIEW_WAIT))
			{
				echo 'Veuillez patienter ' . (MIN_VIEW_WAIT - time() + $_SESSION['onlyview_time']) . 's avant de pouvoir obtenir de nouvelles variables.<br />';
				exit;
			}
			$_SESSION['onlyview_time'] = time();
		}
	}
	else
	{
		if(AutoLoginUser() == false)
		{
			if(isset($_POST['islogin']) && isset($_POST['username']) && isset($_POST['sha1']))
			{
				$loginerror = LoginUser($_POST['username'], $_POST['sha1']);
				if($loginerror === true) unset($loginerror);
			}
		}
		else if(!$mcuser->isadmin)
			LogUser();
		if(isset($_POST['logout']))
			LogoutUser();
	}
}

?>